﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security;
using BLL;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class Controls_FileUpload2Control : System.Web.UI.UserControl
{
    public string FolderUpload { get; set; }

    public object ObjectControlMax { get; set; }
    public object ObjectControlMin { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.User.Identity.IsAuthenticated)
            throw new SecurityException("Bạn không có quyền upload file.");

         lblFeedbackKO.Visible = false;
         lblFeedbackOK.Visible = false;
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (filUpload.PostedFile != null && filUpload.PostedFile.ContentLength > 0)
        {
            try
            {
                string url = this.Page.Request.ApplicationPath;
                if (!url.EndsWith("/"))
                    url=url + "/";

                string dirUrlMax = url + GetFolderMax(FolderUpload);
                string dirUrlMin = url + GetFolderMin(FolderUpload);
                string dirPathMax = Server.MapPath(dirUrlMax);
                string dirPathMin = Server.MapPath(dirUrlMin);

                if (!Directory.Exists(dirPathMax))
                    Directory.CreateDirectory(dirPathMax);
                if (!Directory.Exists(dirPathMin))
                    Directory.CreateDirectory(dirPathMin);

                string fileUrlMin = dirUrlMin + "/" + filUpload.PostedFile.FileName;
                string fileUrlMax = dirUrlMax + "/" + filUpload.PostedFile.FileName;


                //Upload file len host
                //filUpload.PostedFile.SaveAs(Server.MapPath(fileUrl));

                System.Drawing.Image imageFull = System.Drawing.Image.FromStream(filUpload.PostedFile.InputStream);
                //Xác định dài X rộng

                int widthNho = 170;
                int widthLon = 400;
                int MaxHeightNho = 170;
                int MaxHeightLon = 400;
                if (imageFull.Width <= widthNho)
                {
                    widthNho = imageFull.Width;
                }
                if (imageFull.Width <= widthLon)
                {
                    widthLon = imageFull.Width;
                }
                int heightNho = imageFull.Height * widthNho / imageFull.Width;
                int heightLon = imageFull.Height * widthLon / imageFull.Width;
                if (heightNho > MaxHeightNho)
                {   
                    heightNho = MaxHeightNho;
                }
                if (heightLon > MaxHeightLon)
                {
                    heightLon = MaxHeightLon;
                }

                System.Drawing.Image imageLon = new Bitmap(widthLon, heightLon, imageFull.PixelFormat);
                System.Drawing.Image imageNho = new Bitmap(widthNho, heightNho, imageFull.PixelFormat);

                Graphics graphicLon = Graphics.FromImage(imageLon);
                graphicLon.CompositingQuality = CompositingQuality.HighQuality;
                graphicLon.SmoothingMode = SmoothingMode.HighQuality;
                graphicLon.InterpolationMode = InterpolationMode.HighQualityBicubic;
                Rectangle rectangleLon = new Rectangle(0, 0, widthLon, heightLon);
                graphicLon.DrawImage(imageFull, rectangleLon);

                Graphics graphicNho = Graphics.FromImage(imageNho);
                graphicNho.CompositingQuality = CompositingQuality.HighQuality;
                graphicNho.SmoothingMode = SmoothingMode.HighQuality;
                graphicNho.InterpolationMode = InterpolationMode.HighQualityBicubic;
                Rectangle rectangleNho = new Rectangle(0, 0, widthNho, heightNho);
                graphicNho.DrawImage(imageFull, rectangleNho);

                imageLon.Save(Server.MapPath(fileUrlMax), ImageFormat.Png);
                imageNho.Save(Server.MapPath(fileUrlMin), ImageFormat.Png);
                imageFull.Dispose();

                if ((ObjectControlMax != null) && (ObjectControlMax is TextBox))
                {
                    TextBox txtImageUrl = (TextBox)ObjectControlMax;
                    txtImageUrl.Text = fileUrlMax;
                }

                if ((ObjectControlMin != null) && (ObjectControlMin is TextBox))
                {
                    TextBox txtImageUrlMin = (TextBox)ObjectControlMin;
                    txtImageUrlMin.Text = fileUrlMin;
                }

                lblFeedbackOK.Visible = true;
                lblFeedbackOK.Text = "Upload file thành công : ";
                lblFeedbackOK.Text += fileUrlMax +"<br/>";
                lblFeedbackOK.Text += "Image Min: " + fileUrlMin;


            }
            catch (Exception ex)
            {
                lblFeedbackKO.Visible = true;
                lblFeedbackKO.Text = ex.Message;
            }
        }
    }

    string GetFolderMax(string folder)
    {
        string tr = "";
        switch (folder)
        {
            case "Departments":
                tr = "Upload/Departments";
                break;
            case "Products":
                tr = "Upload/Products";
                break;
            case "Categories":
                tr = "Upload/Categories";
                break;
            case "Articles":
                tr = "Upload/Articles";
                break;
            default:
                tr = "Upload";
                break;
        }
        return tr;
    }

    string GetFolderMin(string folder)
    {
        string tr = "";
        switch (folder)
        {
            case "Departments":
                tr = "Upload/Departments/THUMB";
                break;
            case "Products":
                tr = "Upload/Products/THUMB";
                break;
            case "Categories":
                tr = "Upload/Categories/THUMB";
                break;
            case "Articles":
                tr = "Upload/Articles/THUMB";
                break;
            default:
                tr = "Upload";
                break;
        }
        return tr;
    }
}
